package com.dx.wlmq.controller;

import com.alibaba.fastjson.JSON;
import com.dx.wlmq.domain.SysUser;
import com.dx.wlmq.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;

/**
 * @ClassName SysUserController
 * @Description 控制层
 * @Author 12994
 * @Date 2024/10/20 11:35
 * @Version 1.0
 **/
// 提示Spring框架可以通过该类创建一个bean实例, 并注入到IoC容器中
@Controller
// 监听路由
@RequestMapping("/sysUser")
public class SysUserController {

    @Autowired
    // 提示控制层可以注入sysUserService这个业务层的实现
    @Qualifier("sysUserService")
    private SysUserService sysUserService;

    /**
     * 获取完整的用户列表
     */
    // 监听二级路由
    @RequestMapping(value = "/listSysUser", method = RequestMethod.GET, produces = "application/json; charset=utf-8;")
    // 提示bean容器可以直接将相应结果返回给客户端
    @ResponseBody
    public String listSysUser(HttpServletResponse response) {
        response.setContentType("application/json; charset=utf-8;");
        return JSON.toJSONString(sysUserService.listSysUsers());
    }

    /**
     * 通过用户id查询一个用户
     */
    @RequestMapping(value = "/findUserByUserId", method = RequestMethod.GET, produces = "application/json; charset=utf-8;")
    @ResponseBody
    public String findUserByUserId(Long userId, HttpServletResponse response) {
        response.setContentType("application/json; charset=utf-8;");
        return JSON.toJSONString(sysUserService.findUserByUserId(userId));
    }

    /**
     * 添加一个用户
     */
    @RequestMapping(value = "/addSysUser", method = RequestMethod.POST, produces = "application/json; charset=utf-8;")
    @ResponseBody
    public String addSysUser(@RequestBody SysUser sysUser, HttpServletResponse response) {
        response.setContentType("application/json; charset=utf-8;");
        return JSON.toJSONString("添加了" + sysUserService.addSysUser(sysUser) + "条数据!");
    }

    /**
     * 更新一个用户
     */
    @RequestMapping(value = "/updateSysUser", method = RequestMethod.PUT, produces = "application/json; charset=utf-8;")
    @ResponseBody
    public String updateSysUser(SysUser sysUser, HttpServletResponse response) {
        response.setContentType("application/json; charset=utf-8;");
        return JSON.toJSONString("更新了" + sysUserService.updateSysUser(sysUser) + "条数据!");
    }

    /**
     * 通过用户id删除一个用户
     */
    @RequestMapping(value = "/removeSysUserByUserId", method = RequestMethod.DELETE, produces = "application/json; charset=utf-8;")
    @ResponseBody
    public String removeSysUserByUserId(Long userId, HttpServletResponse response) {
        response.setContentType("application/json; charset=utf-8;");
        return JSON.toJSONString("删除了" + sysUserService.removeSysUserByUserId(userId) + "条数据!");
    }

    /**
     * 使用用户名的模糊查询
     */
    @RequestMapping(value = "/listSysUsersLike", method = RequestMethod.GET, produces = "application/json; charset=utf-8;")
    @ResponseBody
    public String listSysUsersLike(String userName, HttpServletResponse response) {
        response.setContentType("application/json; charset=utf-8;");
        return JSON.toJSONString(sysUserService.listSysUsersLike(userName));
    }

    @RequestMapping(value = "/delSysUserByList", method = RequestMethod.POST, produces = "application/json; charset=utf-8;")
    @ResponseBody
    // 接收实体类(包装类)的ArrayList => 前端json like的object里面的是items是每一个对象的json like
    // ArrayList<Long> <== [1,2,3]
    // ArrayList<SysUser>  <==  ["sysUserId":1,"sysUserId":2,"sysUserId":3]
    public String delSysUserByList(@RequestBody ArrayList<SysUser> userArrayList, HttpServletResponse response) {
        System.out.println(userArrayList);
        response.setContentType("application/json; charset=utf-8;");
        return JSON.toJSONString(userArrayList);
    }
}
